<?php

require_once 'classes/LightPressPlugin.php';

class ArchivesListExtended extends LightPressPlugin {
    
    var $constructor_args = array(
        'limit_to'=>'Run the plugin only for this file',
        // 'post_separator'=>'Character(s) to use when joining post titles',
        'unique_title'  => 'Set the page title to this string if not empty'
        );
    var $limit_to = 'list_archives.php';
    // var $post_separator = '&#8214;';
    var $unique_title = 'Archives &raquo; {option_blogname}';
    
    var $default_context = LP_CONTEXT_INDEX;
    var $description = 'Display archive months and their posts titles';
    
    
    var $active = true;
    var $hooks = array('pre_render');
    var $_extra_months = 3;
    
    function ArchivesListExtended(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run && !empty($this->limit_to) && basename($_SERVER['PHP_SELF']) != $this->limit_to)
            $this->active = false;
        
    }
    
    function run($hook, &$hook_data) {
        $f =& $this->_frontend;
        $tpl =& $f->tpl;
        $db =& $f->db;
        
        // $sep = $this->post_separator;
        
        $tpl->setFile('plugin_archiveslistextended', 'plugins/archives_list_extended.xml');
        
        $archives = $this->_frontend->getArchives();
        
        $q = "select id, post_name, post_title, UNIX_TIMESTAMP(post_date) as tstamp, post_category, " .
            "concat(year(post_date), lpad(month(post_date), 2, '0')) as archive " .
            "from " . $f->tables['posts'] . " " .
            "where post_status='publish' and post_date_gmt <= '" . $f->where_date . "' " .
            "order by post_date desc";
        $db->query($q);
        
        $opt_url = $f->options['url'];
        $archive = null;
        foreach ($db->all() as $post) {
            if ($archive != $post['archive']) {
                if (!is_null($archive))
                    $archives[$archive]['posts'] = '<li class="first">' . implode("</li><li>", $posts) . '</li>';
                $posts = array();
                $archive = $post['archive'];
            }
            $posts[] = sprintf('<a href="%s/%s">%s</a>',
                               $opt_url,
                               $f->getPermalink($post['id'], $post['post_name'], $post['tstamp'], $post['post_category']),
                               htmlspecialchars($post['post_title'], ENT_QUOTES));
        }
        $archives[$archive]['posts'] = '<li class="first">' . implode("</li><li>", $posts) . '</li>';
//        require_once 'Var_Dump.php';
//        Var_Dump::display($archives);
        
        $tpl->parseBlock(
            'PLUGIN_ARCHIVESLISTEXTENDED_ARCHIVE', 'plugin_archiveslistextended_archive',
            $archives, 'plugin_archiveslistextended');
        $tpl->parse('PLUGIN_ARCHIVESLISTEXTENDED', 'plugin_archiveslistextended');
        
        if ($this->unique_title)
            $tpl->setVar('title', $this->unique_title);
        
    }
    
}

?>
